package net.ycxinxing.mcpebox.ui;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.ycxinxing.mcpebox.CallbackBundle;
import net.ycxinxing.mcpebox.Pro.R;
import net.ycxinxing.mcpebox.baseActivity;
import net.ycxinxing.mcpebox.ui.AddonMgr;
import net.ycxinxing.mcpebox.utils.OpenFileDlg;
import net.ycxinxing.mcpebox.utils.Utils;

public class AddonMgr extends baseActivity
{
	private ListView list;
	private SwipeRefreshLayout layout;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_fabactivity);
		layout = (SwipeRefreshLayout)findViewById(R.id.viewfabactivitySwipeRefreshLayout1);
		list = (ListView)findViewById(R.id.viewfabactivityListView1);
		((FloatingActionButton)findViewById(R.id.viewfabactivityFloatingActionButton1)).setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View p1)
				{
					showDialog(1234);
					// TODO: Implement this method
				}
			});
		list.setOnItemClickListener(new OnItemClickListener(){
				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					new File("/data/data/" + AddonMgr.this.getPackageName() + "/addons/" + ((TextView)p2).getText().toString()).delete();
					refish();
					// TODO: Implement this method
				}
			});
		layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
				@Override
				public void onRefresh()
				{
					refish();
					// TODO: Implement this method
				}
			});
		refish();
	}
	public void refish()
	{
		layout.setRefreshing(true);
		new Handler().postDelayed(new Runnable(){

				@Override
				public void run()
				{
					List<String> addonlist=new ArrayList<String>();
					File[] files=new File("/data/data/" + getPackageName() + "/addons/").listFiles();
					for (File file:files)
					{
						addonlist.add(file.getName());
					}
					ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(AddonMgr.this, android.R.layout.simple_spinner_item, addonlist);
					list.setAdapter(arrayAdapter);
					layout.setRefreshing(false);
				}
			}, 70);
	}
	@Override
	protected Dialog onCreateDialog(int id, Bundle args)
	{
		if (id == 1234)
		{
			Map<String, Integer> images = new HashMap<String, Integer>();
			// 下面几句设置各文件类型的图标， 需要你先把图标添加到资源文件夹
			images.put(OpenFileDlg.sRoot, R.drawable.fi_dir);	// 根目录图标
			images.put(OpenFileDlg.sParent, R.drawable.fi_dir);	//返回上一层的图标
			images.put(OpenFileDlg.sFolder, R.drawable.fi_dir);	//文件夹图标
			images.put("so", R.drawable.fi_file);	//文件图标
			images.put(OpenFileDlg.sEmpty, R.drawable.fi_file);
			Dialog dialog = OpenFileDlg.createDialog(1234, this, "打开文件", new CallbackBundle() {
					@Override
					public void callback(Bundle bundle)
					{
						try
						{
							Utils.copyFile(new File(bundle.getString("path")), new File("/data/data/" + AddonMgr.this.getPackageName() + "/addons/" + new File(bundle.getString("path")).getName()));
							refish();
						}
						catch (Exception e)
						{

						}
						// TODO: Implement this method
					}
				}, ".so;", images);
			return dialog;
		}
		return super.onCreateDialog(id, args);
	}
}
